package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerNeedAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerNeed;
import com.isunland.managesystem.entity.CustomerNeedProductOriginal;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/17 16:45
 */
public class CustomerNeedProductFragment extends BaseListFragment {

  public static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.CustomerNeedProductFragment.EXTRA_VALUE";
  private CustomerNeedAdapter mAdapter;
  private ArrayList<CustomerNeed> mList;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.customer_need_list);
    setIsPaging(true);
    mList = new ArrayList<>();
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty parame = new ParamsNotEmpty();
    parame.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    parame.put("needProductDesc", "");
    parame.put("customerName", "");
    parame.put("orderField", "REG_DATE");
    parame.put("orderSeq", "desc");
    parame.put("type", "mobile");
    return parame.getParams();
  }

  @Override public String getUrl() {
    return ApiConst.URL_CUSTOMER_NEED;
  }

  @Override public void resolveResponse(String arg0) {

    CustomerNeedProductOriginal original =
        new Gson().fromJson(arg0, CustomerNeedProductOriginal.class);
    if (original != null && Base.SUCCESS == original.getResult()) {
      if (getCurrentPage() == 1) {
        mList.clear();
      }
      mList.addAll(original.getRows());
      if (mAdapter == null) {
        mAdapter = new CustomerNeedAdapter(getActivity(), mList);
        setListAdapter(mAdapter);
      } else {
        mAdapter.notifyDataSetChanged();
      }
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  public void onListItemClick(ListView l, View v, int position, long id) {
    CustomerNeed customerNeed = ((CustomerNeedAdapter) getListAdapter()).getItem(position - 1);
    Intent i = new Intent();
    i.putExtra(CustomerNeedProductFragment.EXTRA_VALUE, customerNeed);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }
}
